<?php

namespace app\common\event;


use app\common\libs\InsideApi;
use app\common\loader\ModelMerchant;
use yiqiniu\inside_sdk\message_libs\MessageBuilder;

/**
 *  导购注册成功后
 */
class RegisterSuccessEventHandler
{

    /**
     * 注册成功, 通知管理员
     * @param array $params ['mer_id'=>'','open_id'=>'xxxx','name'=>'xxxcc','mobile'=>'1222232','title'=>'xx','remark223']
     * @return void
     * @throws \Exception
     */
    public function handle(array $params)
    {
        $default = [
            'mer_id' => '',
            'open_id' => '',
            'name' => '',
            'mobile' => '',
            'title' => '有新的导购注册,请及时审核~',
            'remark' => '感谢使用平台'
        ];
        $params = array_merge($default, $params);
        if (empty($params['mer_id']) || empty($params['name']) || empty($params['mobile'])) {
            return;
        }


        // 如果管理员的ID空直接返回
        $openidAndMobile = ModelMerchant::MerchantInfo()->getOpenidAndMobile($params['mer_id']);
        if (empty($openidAndMobile['open_id'])) {
            return;
        }

        try {
            $data = MessageBuilder::getInstance()
                ->setReceivers(1, $openidAndMobile['open_id'])
                ->setUserType(1)
                ->setTpl('sale_man_register_success')
                ->setData([
                    'title' => $params['title'],
                    'name' => $params['name'],
                    'mobile' => $params['mobile'],
                    'time' => date('Y-m-d H:i:s'),
                    'remark' => $params['remark']
                ])->getData();
            InsideApi::Message()->Send($data);
        } catch (\Exception $e) {

        }

    }
}